<?php
$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/luckyball.cc/secure";
include($_SERVER["DOCUMENT_ROOT"]."/modules/mysql_wrappers.lib.php");
include($_SERVER["DOCUMENT_ROOT"]."/crontab/AllWeb.inc.php");
global $_Conn;

$_BetDBHost = "192.168.5.202";
$_Conn = "_ConnMaster";
global $_ConnMaster;

$_BetDBUser = "kacino";
$_BetDBPass = "cyriac_0426";
$_BetDBName = "twIPs";
$_BetWeb = $_SERVER["HTTP_HOST"];

if (! $$_Conn = @mysql_connect($_BetDBHost, $_BetDBUser, $_BetDBPass)) {
	?>
	系統維護中，請稍候再登入！不便之處，敬請見諒！！
	<!--系統維護中, 請<a href="javascript:location.reload();">重新整理</a><P>//-->
	<?php
	$email = "service@".$_BetWeb;
	$subject = " mysql_connect error (".$_BetWeb.")";
	$content = "連線錯誤：無法連線至資料庫主機！\nDBSite".$_DBSite."\nPage: ".$_SERVER["PHP_SELF"]."\n";
	mail($email, $subject, $content, "From:".$_BetWeb."<apache@secure.".$_BetWeb.">");
	exit();
}

if (! @PMA_mysql_select_db($_BetDBName, $$_Conn)) {
	?>
	系統維護中，請稍候再登入！不便之處，敬請見諒！
	<!--系統忙碌中, 請稍候片刻, 或立即<a href="javascript:location.reload();">重新整理</a>.//-->
	<script>
	function abc() {
		location.reload();
	}
	setInterval(abc,60000);
	</script>
	<?php 
	$email = "service@".$_BetWeb;
	$subject = "mysql_select_db error (".$_BetWeb.")";
	$content = "連線錯誤：無法連結資料庫！\nDBSite".$_DBSite."\nPage: ".$_SERVER["PHP_SELF"]."\n".PMA_mysql_error($$_Conn);
	$content .= "\nAccount=".$_SESSION["MemberInfo"] -> Account;
	mail($email, $subject, $content, "From:".$_BetWeb."<apache@secure.".$_BetWeb.">");
	exit();
}

//載入使用者IP
if($_GET["isallow"] != "yes@us"){
	exit("^_____^");
}

$UserIP = trim($_GET["raddr"]);//$_SERVER["REMOTE_ADDR"];
//if(!ereg("[1-9]\.){3}([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])/", $UserIP)){
//	exit($UserIP." is not IP !");
//}

$strSQL = "select ip from `passIPs` where ip = '".$UserIP."' ";
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "System Error!!";
	include($_SERVER["DOCUMENT_ROOT"]."/error.php");
	exit();
}

if(PMA_mysql_num_rows($RS) == 0){
	$strSQL = "insert into `passIPs`(ip) values('".$UserIP."')";
	if(!PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "System Error!!";
		include($_SERVER["DOCUMENT_ROOT"]."/error.php");
		exit();
	}
}

//允許通過IP
$strSQL1 = "";
$arrSQL2 = array();
$strSQL = "select `ID`, `unit`, `ip` from `passIPs` where 1";
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "System Error!!";
	include($_SERVER["DOCUMENT_ROOT"]."/error.php");
	exit();
}

$arrCon = array();
if(PMA_mysql_num_rows($RS) > 0){
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrCon[] = "(".$Rows -> ID.", '".addslashes($Rows -> unit)."', '".$Rows -> ip."')";
	}
	$strSQL1 = "INSERT INTO `passIPs`(`ID`, `unit`, `ip`) VALUES ".join(", ", $arrCon);
}
//封鎖IP
$strSQL = "select `ID`, `unit`, `ip` from `ipTable` where 1";
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "System Error!!";
	include($_SERVER["DOCUMENT_ROOT"]."/error.php");
	exit();
}

$arrCon = array();
$i = 1;
if(PMA_mysql_num_rows($RS) > 0){
	while($Rows = PMA_mysql_fetch_object($RS)){
		if($i % 10000 == 0){
			$arrSQL2[] = "INSERT INTO `ipTable`(`ID`, `unit`, `ip`) VALUES ".join(", ", $arrCon);
			$arrCon = array();
		}
		$arrCon[] = "(".$Rows -> ID.", '', '".$Rows -> ip."')";
		$i++;
	}
	$arrSQL2[] = "INSERT INTO `ipTable`(`ID`, `unit`, `ip`) VALUES ".join(", ", $arrCon);
}

foreach($arrWeb as $strWeb){
	print $strWeb."  Time: ".date("Y-m-d H:i:s")."<br>\n";
	$_isDBExist = "no";
	$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
	include($_SERVER["DOCUMENT_ROOT"]."/db.inc.php");
	if($_strLogErr != ""){
		print $strLogError.$_strLogErr;
		print $strWeb."  Time: ".date("Y-m-d H:i:s")." End<br><br>\n\n";
		continue;
	}
	//允許通過IP
	if(!PMA_mysql_query("TRUNCATE TABLE `passIPs`", $_ConnMaster)){
		print "SQL: ".$strSQL."<br>\n";
		print "Error: ".mysql_error($_ConnMaster)."<br>\n";
	}
	if(!PMA_mysql_query($strSQL1, $_ConnMaster)){
		print "SQL: ".$strSQL."<br>\n";
		print "Error: ".mysql_error($_ConnMaster)."<br>\n";
	}
	/*//封鎖IP
	if(!PMA_mysql_query("TRUNCATE TABLE `ipTable`", $_ConnMaster)){
		print "SQL: ".$strSQL."<br>\n";
		print "Error: ".mysql_error($_ConnMaster)."<br>\n";
	}
	if(sizeof($arrSQL2) > 0){
		foreach($arrSQL2 as $strSQL2){
			if(!PMA_mysql_query($strSQL2, $_ConnMaster)){
				print "SQL: ".$strSQL."<br>\n";
				print "Error: ".mysql_error($_ConnMaster)."<br>\n";
			}
		}
	}*/
	print $strWeb."  Time: ".date("Y-m-d H:i:s")." End<br><br>\n\n";
}
//print $strSQL1."<br>\n";
//print $strSQL2."<br>\n";

$GLOBALS["errormessage"] = "您的 IP (". $UserIP.") 已經加入！";
include($_SERVER["DOCUMENT_ROOT"]."/error.php");
exit();
?>